<?php

session_start();
require_once("include/auth.php");


$currentUser = current_user();

$studentName = $_GET['student_name'];

$permAddress = $_GET['perm_address'];
$email = $_GET['email'];
$dob = $_GET['dob'];
$gender = $_GET['gender'];
$address = $_GET['address'];
$contactNO = $_GET['contact_no'];
$email = $_GET['email'];
$major = $_GET['major'];
$degree = $_GET['degree'];
$errors = $_GET['errorMsg'];
$success = $_GET['successMsg'];
$errorMsg = array(
    "1" => "Missing required field: Name",
    "2" => "Missing required field: Date of Birth",
    "3" => "Missing required field: Gender",
    "4" => "Missing required field: Address",
    "5" => "Missing required field: Permanent Address",
    "6" => "Missing required field: Contact Number",
    "7" => "Missing required field: Email");

$displayError = array();
$errorArray = explode ( ",", $errors );
foreach($errorArray as $element) {
    $displayError[] = $errorMsg[$element];

}

$eduHistory = array();
//if no current user then redirect to login screen
if (!$currentUser){
    redirect("signin.php");
}
else{
    if ($studentName == ""){
        $studentName = $currentUser['Name'];
    }
    if ($gender == ""){
        $gender= $currentUser['Gender'];
    }
    if ($address == ""){
        $address = $currentUser['Address'];
        //$address = "Can't find address";
    }
    if ($permAddress == ""){
        $permAddress = $currentUser['Permanent_Address'];
    }
    if ($email == ""){
        $email = $currentUser['Email_ID'];
    }
    if ($contactNo == ""){
        $contactNo=$currentUser['Contact_No'];
    }
    if ($birthDate == ""){
        $birthDate = $currentUser['Date_Of_Birth'];
    }
    if ($major == ""){
        $student = getStudent($currentUser['Username']);
        $major = $student->Major;
        $degree = $student->Degree;
    }
    $tutor = $currentUser['Tutor'];

    $majors = getDepartments();

    $eduHistory = getEduHistory($currentUser['Username']);

    if( sizeof($eduHistory) == 0) {
        $eduHistory[] = array("Name_Of_School" => "", "Year_Graduation" => "", "Degree" => "", "Major" => "", "GPA" => "");
    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Personal Information</title>
    <link rel="stylesheet" type="text/css" href="view.css" media="all">
    <script type="text/javascript" src="view.js"></script>
    <script type="text/javascript" src="calendar.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("select#classes").attr("disabled","disabled");
            $("#tutor_yes").change(function(){
                loadTutorClasses();
            });

            $("#tutor_no").change(function(){
                clearTutorClasses();
            });

            $("#add_education_btn").click(function(){
                if ($("input[name='name_first_inst[]']").length < 3){
                    $('#add_education_btn').parent()
                        .before( doClone("#name_item") )
                        .before( doClone("#major_item") )
                        .before( doClone("#degree_item") )
                        .before( doClone("#grad_item") )
                        .before( doClone("#gpa_item") );
                }
                return false;
            });

            if ( $('input[name=tutor]:checked').val()=='1') {
                loadTutorClasses();
            }

        });

        function doClone( selector ) {
            var c = $(selector).clone();
            c.find("input").val("");
            c.find("select>option").first().attr("selected", "yes");
            return c;
        }

        function loadTutorClasses() {
            dynamicDropDown("select#classes","get_classes_to_tutor.php",{}, "Course_Title", "Course_Title");
        }


        function clearTutorClasses(){
            $("select#classes").empty();
        }



        function dynamicDropDown(drop_down_id, url, params, value_name, display_name  ){

            $(drop_down_id).attr("disabled","disabled");
            $(drop_down_id).html("<option>wait...</option>");

            $.get(url, params, function(data){
                $(drop_down_id).removeAttr("disabled");
                $(drop_down_id).empty();
                for (var i=0;i<data.length;i++)
                {
                    var item = data[i];
                    var optionNode = $('<option></option>').attr('value', item[value_name]).text(item[display_name]);
                    $(drop_down_id).append(optionNode);
                }

            });

        }
    </script>
</head>
<body id="main_body" >

<img id="top" src="top.png" alt="">
<div id="form_container">

<h1><a>Personal Information</a></h1>
<form id="form_528101" class="appnitro"  method="post" action="personal_info_action.php">
<div class="form_description">
    <h2>Personal Information</h2>
    <p class="error"><?= join("<br/>",$displayError) ?></p>
    <p style="color:green;"><?=$success?></p>
</div>
<ul >

<li id="li_1" >
    <label class="description" for="student_name">Name </label>
    <div>
        <input id="student_name" name="student_name" class="element text medium" type="text" maxlength="255" value="<?=$studentName?>"/>
    </div>
</li>		<li id="li_5" >
    <label class="description" for="dob">Date of Birth </label>
		<div>
			<input id="dob" name="dob" class="element text" size="12" maxlength="10" value="<?=$birthDate?>"type="text">
			<label for="dob">YYYY-MM-DD</label>
        </div>


</li>		<li id="li_4" >
    <label class="description" for="gender">Gender </label>
    <div>
        <select class="element select small" id="gender" name="gender">
            <option value="" <?=($gender == '') ? "selected='selected'" : "" ?>></option>
            <option value="M" <?=($gender == 'M') ? "selected='selected'" : "" ?>>Male</option>
            <option value="F" <?=($gender == 'F') ? "selected='selected'" : "" ?>>Female</option>

        </select>
    </div>
</li>		<li id="li_6" >
<label class="description" for="address">Address </label>

<div>
    <input id="address" name="address" class="element text large" value="<?=$address?>" type="text">
    <label for="address">Street Address</label>
</div>

</li>		<li id="li_7" >
<label class="description" for="perm_address">Permanent Address </label>

<div>
    <input id="perm_address" name="perm_address" class="element text large" value="<?=$permAddress?>" type="text">
    <label for="perm_address">Street Address</label>
</div>


</li>		<li id="li_8" >
    <label class="description" for="contact_no">Contact Number </label>
		<div>
			<input id="contact_no" name="contact_no" class="element text small" size="12" maxlength="10" value="<?=$contactNo?>" type="text">
			<label for="contact_no">e.g. 55555555555</label>
		</div>

</li>		<li id="li_9" >
    <label class="description" for="email">Email Address </label>
    <div>
        <input id="email" name="email" class="element text medium" type="text" maxlength="255" value="<?=$email?>"/>
    </div>
</li>		<li id="li_15" >
    <label class="description">Willing to Tutor? </label>
		<span>

			<input id="tutor_yes" name="tutor" class="element radio" type="radio" value="1" <?=($tutor == '1') ? "checked" : "" ?>/>
            <label class="choice" for="tutor_yes">Yes</label>
            <input id="tutor_no" name="tutor" class="element radio" type="radio" value="2" <?=($tutor == '0') ? "checked" : "" ?>/>
            <label class="choice" for="tutor_no">No</label>

		</span>
</li>		<li id="li_16" >
    <label class="description" for="classes">If yes, select the courses you would like to tutor for (Ctrl-click for multi-select) </label>
    <div>
        <select class="element select medium" id="classes" name="classes[]" multiple="yes">
            <option value="" selected="selected"></option>

        </select>
    </div>
</li>		<li id="li_17" >
    <label class="description" for="major">Major </label>
    <div>
        <select class="element select medium" id="major" name="major">
            <option value="" <?=($major == '') ? "" : "selected='selected'" ?>></option>
            <?php foreach($majors as $stockMajor) { ?>
                <?php if($major == $stockMajor->Dept_Name){ ?>
                        <option value="<?= $stockMajor->Dept_Name ?>" selected="selected"><?= $stockMajor->Dept_Name ?></option>
                <?php } else { ?>
                        <option value="<?= $stockMajor->Dept_Name ?>"><?= $stockMajor->Dept_Name ?></option>
                <?php } ?>
            <?php } ?>

        </select>
    </div>
</li>		<li id="li_18" >
    <label class="description" for="degree">Degree </label>
    <div>
        <select class="element select medium" id="degree" name="degree">
            <option value="" <?=($degree == '') ? "selected='selected'" : "" ?>></option>
            <option value="Bachelors" <?=($degree == 'Bachelors') ? "selected='selected'" : "" ?>>Bachelors</option>
            <option value="Masters" <?=($degree == 'Masters') ? "selected='selected'" : "" ?>>Masters</option>
            <option value="PhD" <?=($degree == 'PhD') ? "selected='selected'" : "" ?>>PhD</option>

        </select>
    </div>
</li>		<li class="section_break">
    <h3>Previous Education</h3>
    <p>Select up to 3</p>
</li>
<?php foreach( $eduHistory as $education ) { ?>
<li id="name_item" >
    <label class="description" for="name_first_inst">Name of Institution Attended</label>
    <div>
        <input id="name_first_inst" name="name_first_inst[]" class="element text medium" type="text" maxlength="255" value="<?= $education->Name_Of_School ?>"/>
    </div>
</li>
<li id="major_item" >
    <label class="description" for="first_major">Major </label>
    <div>
        <input id="first_major" name="first_major[]" class="element text medium" type="text" maxlength="255" value="<?= $education->Major ?>"/>
    </div>
</li>
<li id="degree_item" >
    <label class="description" for="first_degree">Degree </label>
    <div>
        <select class="element select medium" id="first_degree" name="first_degree[]">
            <option value="" <?= $education->Degree == '' ? 'selected="selected"' : '' ?>></option>
            <option value="Bachelors" <?= $education->Degree == 'Bachelors' ? 'selected="selected"' : '' ?>>Bachelors</option>
            <option value="Masters" <?= $education->Degree == 'Masters' ? 'selected="selected"' : '' ?>>Masters</option>
            <option value="PhD" <?= $education->Degree == 'PhD' ? 'selected="selected"' : '' ?>>PhD</option>
        </select>
    </div>
</li>
<li id="grad_item" >
    <label class="description" for="first_grad">Year of Graduation </label>
    <div>
        <input id="first_grad" name="first_grad[]" class="element text small" type="text" maxlength="255" value="<?= $education->Year_Graduation ?>"/>
    </div>
</li>
<li id="gpa_item" >
    <label class="description" for="first_gpa">GPA </label>
    <div>
        <input id="first_gpa" name="first_gpa[]" class="element text small" type="text" maxlength="255" value="<?= $education->GPA ?>"/>
    </div>
</li>
<?php } ?>




    <li class="buttons">
        <button id="add_education_btn">Add History</button>
    </li>



    <li class="buttons">
    <input type="hidden" name="form_id" value="528101" />

    <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
<div id="footer">
    Sharp Cheddar
</div>
</div>
<img id="bottom" src="bottom.png" alt="">
</body>
</html>